package com.easyadmin.controller;

import com.easyadmin.annotation.GlobalInterceptor;
import com.easyadmin.annotation.VerifyParam;
import com.easyadmin.entity.enums.OperModuleEnum;
import com.easyadmin.entity.enums.PermissionCodeEnum;
import com.easyadmin.entity.po.SysMenu;
import com.easyadmin.entity.query.SysMenuQuery;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.service.SysMenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 菜单表 控制层
 */
@RestController
@RequestMapping("/api/settings")
public class SysMenuController extends ABaseController {

    @Resource
    private SysMenuService sysMenuService;

    /**
     * @Description: 获取所有菜单
     * @auther: 多多洛
     * @date: 12:01 2020/11/21
     * @param: [disabled]
     * @return: AjaxResponseVO
     */
    @RequestMapping("/menuList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.NO_PERMISSION)
    public AjaxResponseVO menuList(Boolean disabled) {
        SysMenuQuery param = new SysMenuQuery();
        param.setOrderBy("sort asc");
        param.setFormate2Tree(true);
        disabled = disabled == null ? false : disabled;
        return getSuccessResponseVO(sysMenuService.findListByParam(param, disabled));
    }

    @RequestMapping("/permissionCodeList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_MENU_EDIT)
    public AjaxResponseVO permissionCodeList() {
        return getSuccessResponseVO(PermissionCodeEnum.getDataList());
    }

    /**
     * @Description: 保存菜单
     * @auther: 多多洛
     * @date: 12:00 2020/11/21
     * @param: [sysMenu]
     * @return: AjaxResponseVO
     */
    @RequestMapping("/saveMenu")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_MENU_EDIT, opObject = OperModuleEnum.OBJECT_MENU, opDescription = "保存菜单，参数信息:#{#param0}")
    public AjaxResponseVO saveMenu(@VerifyParam SysMenu sysMenu) {
        try {
            this.sysMenuService.saveMenu(sysMenu);
            return getSuccessResponseVO(null);
        } catch (Exception e) {
            return getServerErrorResponseVO();
        }
    }


    /**
     * @Description: 删除菜单
     * @auther: 多多洛
     * @date: 12:00 2020/11/21
     * @param: [menuId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("/delMenu")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_MENU_DEL, opObject = OperModuleEnum.OBJECT_MENU, opDescription = "删除菜单，菜单ID:#{#param0}")
    public AjaxResponseVO saveMenu(Integer menuId) {
        try {
            this.sysMenuService.deleteByMenuId(menuId);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            return getServerErrorResponseVO();
        }
    }
}